package Storage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateManipulation {

	public static String timeDifference(Date date1, Date date2) {
		final int secondsInMinute = 60;
		final int secondsInHour = 60 * 60;
		final int secondsInDay = 24 * 60 * 60;
		final int secondsInWeek = 7 * 24 * 60 * 60;
		final int secondsInYear = 356 * 24 * 60 * 60;
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(date1);
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(date2);

		 long milliseconds1 = calendar1.getTimeInMillis();
		    long milliseconds2 = calendar2.getTimeInMillis();
		    long diff = milliseconds2 - milliseconds1;
		    int diffSeconds = (int) (diff / 1000);
		    
		int remainder;
		int years = diffSeconds / secondsInYear;
		remainder = diffSeconds % secondsInYear;
		int weeks = remainder / secondsInWeek;
		remainder = remainder % secondsInWeek;
		int days = remainder / secondsInDay;
		remainder = remainder % secondsInDay;
		int hours = remainder / secondsInHour;
		remainder = remainder % secondsInHour;
		int minutes = remainder / secondsInMinute;
		int seconds = remainder % secondsInMinute;

		String displayYear = (years != 0 ? Integer.toString(years)
				+ " yrs " : "");
		String displayWeek = (weeks != 0 ? Integer.toString(weeks)
				+ " wks " : "");
		String displayDay = (days != 0 ? Integer.toString(days) + " days "
				: "");
		String displayHour = (hours != 0 ? Integer.toString(hours)
				+ " hrs " : "");
		String displayMinute = (minutes != 0 ? Integer.toString(minutes)
				+ " mins " : "");
		String displaySecond = (seconds != 0 ? Integer.toString(seconds)
				+ " secs ago" : "");

		return (displayYear + displayWeek + displayDay + displayHour
				+ displayMinute + displaySecond);
	}

	public static Date addSeconds(Date date, int seconds) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, seconds);
		return cal.getTime();
	}
	public static Date StringToDate(String date) {
		Date mydate = null;
		DateFormat formatter;
		formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		try {
			mydate = ((Date) formatter.parse(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mydate;
	}
	/*
	 * private static Date addMinutes(Date date, int minutes) { Calendar cal =
	 * Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MINUTE,
	 * minutes); return cal.getTime(); } private static Date addHours(Date date,
	 * int hours) { Calendar cal = Calendar.getInstance(); cal.setTime(date);
	 * cal.add(Calendar.MINUTE, hours); return cal.getTime(); }
	 */
}
